<template>
  <div class="container">
    <t-button theme="primary" size="small" @click="handleBack">返回</t-button>
    <div class="book-info">
      <div class="book-name">
        {{ book_info.name }}
      </div>
      <div class="book-info-content">
        <div class="cover-container">
          <t-image class="book-cover" :src="book_info.cover" fit="fill" alt="封面" shape="round" />
        </div>

        <div class="info-container">
          <div class="info-item">
            <div class="label">作者：</div>
            {{ book_info.author }}
          </div>
          <div class="info-item">
            <div class="label">出版社：</div>
            {{ book_info.publisher }}
          </div>
          <div class="info-item">
            <div class="label">出版时间：</div>
            {{ book_info.publish_time }}
          </div>
          <div class="info-item">
            <div class="label">ISBN：</div>
            {{ book_info.isbn }}
          </div>
          <div class="info-item">
            <div class="label">字数：</div>
            {{ book_info.word_count }}
          </div>
        </div>
        <div class="book-summary">
          <p>摘要：{{ book_info.summary }}</p>
        </div>
      </div>
    </div>
    <div class="search-container">
      <t-button v-for="item in button_info" :key="item.name" :href="item.url" target="_blank" size="large">
        {{ item.name }}
      </t-button>
      <t-button theme="primary" size="large" @click="handleEnhance">增强</t-button>
    </div>
    <div class="note-container">

      <t-checkbox-group class="note-checkbox-group" v-model="checked" :options="book_chapter"
        name="city"></t-checkbox-group>
      <div class="note-input">
        <t-textarea class="note-textarea" v-model="note" placeholder="请输入读书笔记"  :autosize="{ minRows: 3, maxRows: 3 }"></t-textarea>
        <div class="note-kanban">
          <t-statistic title="总笔记" :value="82" unit="条" trend="increase" />
          <t-statistic title="总笔记字数" :value="72200" unit="字" trend="increase" />
          <t-statistic title="当前章节笔记" :value="22" unit="条" trend="increase" />
          <t-statistic title="当前章节字数" :value="2200" unit="字" trend="increase" />
        </div>
      </div>


    </div>
    <div class="note-group">
      <div class="item box">
        sadjk sad
      </div>
      <div class="item" v-for="item in 30">sads</div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted,watch } from "vue";
import router from "@/router";
import { getBookByIdAPI } from "@/api/media_book";

const book_id = ref(router.currentRoute.value.params.book_id);

// 初始空数据结构（避免渲染错误）
const book_info = ref({
  _id: "",
  name: "",
  author: "",
  cover: "",
  publisher: "",
  publish_time: "",
  isbn: "",
  word_count: "",
  summary: "",

  urls: {
    douban_home: "",
    dedao_home: "",
    dedao_listen_home: ""
  },
});

const button_info = ref<Array<{ name: string; url: string }>>([]);

// 根据 book_info 生成 button_info
const generateButtonInfo = () => {
  return [
    { name: "豆瓣", url: "https://www.douban.com/search?q=" + encodeURIComponent(book_info.value.name), },
    { name: "得到", url: "https://www.dedao.cn/search/result?q=" + encodeURIComponent(book_info.value.name), },
    { name: "B站", url: "https://search.bilibili.com/all?keyword=" + encodeURIComponent(book_info.value.name), },
    { name: "必应", url: "https://cn.bing.com/search?q=" + encodeURIComponent(book_info.value.name), },
    { name: "百度", url: "https://www.baidu.com/s?wd=" + encodeURIComponent(book_info.value.name), },
    { name: "Google", url: "https://www.google.com/search?q=" + encodeURIComponent(book_info.value.name), },
    { name: "YouTube", url: "https://www.youtube.com/results?search_query=" + encodeURIComponent(book_info.value.name), },
    { name: "豆瓣主页", url: book_info.value.urls.douban_home || "#" },
    { name: "得到主页", url: book_info.value.urls.dedao_home || "#" },
    { name: "听书主页", url: book_info.value.urls.dedao_listen_home || "#" }
  ];
};

const handleBack = () => {
  router.push({ name: "kg-book-list" });
};

// 打开全部链接
const handleEnhance = () => {
  for (const item of button_info.value) {
    window.open(item.url, '_blank');
  }
}

// 笔记输入部分
const note = ref("");
const checked = ref<string[]>([]);
const select_all=ref(false)
const book_chapter = ref([
  '概论', '力的产哼', '运动', '运动的原理', '运动的分类', '运动的作用', '运动的形式', '运动的注意事项', '运动的注意事项', '量子力学', '量子力学的原理', '量子力学的分类', '量子力学的作用', '量子力学的形式', '量子力学的注意事项', '量子力学的注意事项',
])
watch(checked, (newVal, oldVal) => {
  if (newVal.includes('全部')) {
    checked.value = book_chapter.value;
  } else if (oldVal.includes('全部')) {
    checked.value = ['全部'];
  }
})
onMounted(async () => {
  const res = await getBookByIdAPI(String(book_id.value));
  // @ts-ignore
  if (res.code === 200) {
    book_info.value = res.data;
    // 更新按钮信息
    button_info.value = generateButtonInfo();
  }
});
</script>

<style lang="scss" scoped>
.container {
  width: 90%;
  margin: 0 auto;
}

.search-container {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
  /* 防止按钮溢出 */
}

.book-info {
  border: 1px solid #d4e4fc;
  border-radius: 5px;
  padding: 10px;
  margin: 10px 0;

  .book-name {
    color: gold;
    font-size: 22px;
  }

  .book-info-content {
    display: flex;

    .cover-container {
      padding: 10px;

      .book-cover {
        width: 120px;
      }
    }

    .info-container {
      display: flex;
      flex-direction: column;
      min-width: 240px;
      padding: 10px;
      gap: 5px;

      .info-item {
        display: flex;

        .label {
          font-weight: bold;
        }
      }
    }
  }
}

.note-container {
  margin: 10px 0;
  border-radius: 5px;
  padding: 10px;
  background-color: #d4e4fc;

  .note-input {
    display: flex;

    .note-textarea {
      width: 60%;
    }

    .note-kanban {
      flex: 1;
      display: flex;
      justify-content: space-between;
      gap:10px;
      margin: 0 20px;
    }
  }
}

.note-group{
margin-top: 10px;
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 5px;
  .item {
    background-color: aqua;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 5px;
    min-height: 50px;
  }
  .box {
    grid-row: 1/3;
    grid-column: 1/3;
  }
}
</style>